//
//  TabBarView.m
//  Kfangchan
//
//  Created by liran on 14/11/5.
//  Copyright (c) 2014年 Beijing Renjuren Network Technology Co., Ltd. All rights reserved.
//

#import "TabBarView.h"

#define BASETAG 100

@interface TabBarView()

@property (copy, nonatomic) NSArray *iconNames;
@property (nonatomic) int selectedIndex;

@end

@implementation TabBarView

- (id)init {
    CGRect frame = CGRectMake(0, 0, 320, 50);
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithHexString:@"#262931"];
        self.selectedIndex = -1;
        self.iconNames = @[@"tabbar_building",
                           @"tabbar_customer",
                           @"tabbar_commission",
                           @"tabbar_personal"];

        float x = 0.0;
        float width = 320/4.0;
        float height = frame.size.height;
        
        
        for(int i = 0; i < self.iconNames.count; i++){
            NSString *normalName = [NSString stringWithFormat:@"%@.png", self.iconNames[i]];
            NSString *downName = [NSString stringWithFormat:@"%@_down.png", self.iconNames[i]];
            UIImage *normalImage = [UIImage imageNamed:normalName];
            UIImage *downImage = [UIImage imageNamed:downName];

            UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(x, 0, width, height);
            
            x += width;
            
            btn.tag = BASETAG + i;
            btn.showsTouchWhenHighlighted = NO;
            btn.exclusiveTouch = YES;
           
            [btn setImage:normalImage forState:UIControlStateNormal];
            [btn setImage:downImage forState:UIControlStateHighlighted];
            [btn setImage:downImage forState:UIControlStateSelected];
            
            [btn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            
            [btn setBackgroundColor:[UIColor clearColor]];
            
            [self addSubview:btn];
        }
    }
    
    return self;
}

- (void)setIndex:(int)index {
    UIButton *btn = (UIButton*)[self viewWithTag:index + 100];
    [self buttonAction:btn];
}

- (void) buttonAction:(UIButton *)sender{
    if([sender isKindOfClass:[UIButton class]]){
        UIButton *btn = sender;
        
        int tag = (int)btn.tag;
        int i = tag - BASETAG;
        
        btn.selected = YES;
        btn.userInteractionEnabled = NO;
        
        if (self.selectedIndex > -1) {
            if(self.selectedIndex + BASETAG != tag){
                UIButton *btnPrev = (UIButton*)[self viewWithTag:self.selectedIndex + BASETAG];
                btnPrev.selected = NO;
                btnPrev.userInteractionEnabled = YES;
            }
        }

        self.selectedIndex = i;
        
        if(self.delegate && [self.delegate respondsToSelector:@selector(didSelectedIndex:)]){
            [self.delegate didSelectedIndex:self.selectedIndex];
        }
    }
}

@end
